import 'package:flutter/material.dart';
import 'package:lib_uikit/resource/gt_iconfont.dart';
import 'package:lib_uikit/theme/gt_colors_v3.dart';
import 'package:lib_uikit/theme/gt_theme_provider.dart';
import 'package:lib_uikit/utils/gt_rtl_property.dart';
import 'package:lib_uikit/utils/gt_text_style.dart';
import 'package:lib_uikit/widgets/gt_alert_v3.dart';
import 'package:lib_uikit/widgets/gt_button_v3.dart';

class GTCenterAlertWidget extends StatelessWidget {
  final bool isShowClose;
  final String title;
  final String contentText;

  ///取消按钮
  final String leftText;

  ///确认按钮
  final String rightText;
  final GestureTapCallback? onLeftTap;
  final GestureTapCallback? onRightTap;
  final bool? confirmButtonExpanded;
  final bool? isButtonVertical;
  final String? alertText;

  const GTCenterAlertWidget({
    Key? key,
    required this.contentText,
    this.title = '',
    this.isShowClose = false,
    this.leftText = '',
    this.rightText = '',
    this.onLeftTap,
    this.onRightTap,
    this.confirmButtonExpanded,
    this.isButtonVertical,
    this.alertText,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        child: Container(
          margin: const EdgeInsets.symmetric(horizontal: 24),
          decoration: BoxDecoration(
            color: GTColorsV3.cmpt16.color,
            borderRadius: BorderRadius.circular(8),
          ),
          child: _renderContains(context),
        ),
      ),
    );
  }

  Widget _renderContains(BuildContext context) {
    if (isShowClose) {
      return Stack(
        children: [
          _renderContentItem(context),
          Positioned(
            top: 8,
            right: GTThemeProvider.instance.isRTL() ? null : 8,
            left: GTThemeProvider.instance.isRTL() ? 8 : null,
            child: GestureDetector(
              child: Padding(
                padding: const EdgeInsets.all(8),
                child: Icon(
                  GTIconFont.e89b,
                  size: 16,
                  color: GTColorsV3.icon3.color,
                ),
              ),
              onTap: () {
                if (Navigator.canPop(context)) {
                  Navigator.of(context).pop();
                }
              },
            ),
          ),
        ],
      );
    }
    return _renderContentItem(context);
  }

  Widget _renderContentItem(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(24.0),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Padding(
            padding: GTEdgeInsetsOnly(right: isShowClose ? 20 : 0, bottom: 16),
            child: Text(
              title,
              style: gtTextStyleW500(fontSize: 16, color: GTColorsV3.text1.color),
            ),
          ),
          if (contentText.isNotEmpty) ...[
            Text(
              contentText,
              style: gtTextStyleW400(
                fontSize: 14,
                color: GTColorsV3.text2.color,
              ).copyWith(height: 1.4),
            ),
            const SizedBox(height: 16),
          ],
          if ((alertText ?? "").isNotEmpty) ...[
            GTAlertV3(
              text: alertText ?? "",
              type: GTAlertTypeV3.yellow,
            ),
            const SizedBox(height: 16),
          ],
          const SizedBox(height: 8),
          _renderButton(context)
        ],
      ),
    );
  }

  Widget _renderButton(BuildContext context) {
    if (leftText.isEmpty) {
      return _renderButtonItem(context, rightText, GTButtonTypeV3.primary, onRightTap);
    }
    if (isButtonVertical ?? false) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          _renderButtonItem(context, rightText, GTButtonTypeV3.primary, onRightTap),
          const SizedBox(height: 8),
          _renderButtonItem(context, leftText, GTButtonTypeV3.secondary, onLeftTap)
        ],
      );
    } else {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          if (confirmButtonExpanded ?? false) ...[
            _renderButtonItem(context, leftText, GTButtonTypeV3.secondary, onLeftTap, isFullWidth: false)
          ] else ...[
            Expanded(
              child: _renderButtonItem(context, leftText, GTButtonTypeV3.secondary, onLeftTap),
            ),
          ],
          const SizedBox(width: 8),
          Expanded(
            child: _renderButtonItem(context, rightText, GTButtonTypeV3.primary, onRightTap),
          )
        ],
      );
    }
  }

  Widget _renderButtonItem(BuildContext context, String title, GTButtonTypeV3 buttonType, GestureTapCallback? onTap,
      {bool? isFullWidth = true}) {
    return GTButtonV3(
        text: title,
        buttonSize: GTButtonSizeV3.medium,
        buttonType: buttonType,
        isFullWidth: isFullWidth,
        paddingHorizontal: (isFullWidth ?? true) ? 0 : 24,
        onPressed: () {
          if (Navigator.canPop(context)) {
            Navigator.of(context).pop();
          }
          onTap?.call();
        });
  }
}
